/*******************************************************************************
 * CBNewsTableViewCell.m
 * ComicsBlog
 *
 * Adrien Bougouin <adrien.bougouin@gmail.com>
 */

#import "CBNewsTableViewCell.h"

@implementation CBNewsTableViewCell

- (id) initWithNews:(CBNews *) news {
  NSUInteger cellStyle = UITableViewCellStyleDefault;
  unsigned long identifier = (unsigned long) [news identifier];
  NSString *reuseIdentifier = [[NSString alloc] initWithFormat:@"%lu",
                                                               identifier];

  self = [super initWithStyle:cellStyle reuseIdentifier:reuseIdentifier];

  if(self) {
    // add a new autorelease pool to the stack
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *resources = [mainBundle bundlePath];
    UIColor *cbColor2 = [[UIColor alloc] initWithRed:(238.0 / 255.0)
                                               green:(140.0 / 255.0)
                                                blue:(0.0 / 255.0)
                                               alpha:1.0];
    NSString *imageFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                               resources,
                                                               CBNewsLoadingImage];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageFilePath];
    UIView *contentView = [self contentView];
    UIView *highlightedBackground = [[UIView alloc] initWithFrame:[contentView bounds]];
    UIImageView *imageView = [self imageView];
    UILabel *textLabel = [self textLabel];

    _foo = [[NSString alloc] initWithString:@"FIX an iOS bug..."];

    [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [textLabel setText:[news title]];
    [textLabel setFont:[UIFont fontWithName:@"Francois One" size:15]];
    [[self textLabel] setLineBreakMode:UILineBreakModeWordWrap];
    [textLabel setNumberOfLines:0];
    [imageView setImage:image];
    [highlightedBackground setBackgroundColor:cbColor2];
    [self setSelectedBackgroundView:highlightedBackground];
    [self loadImageFromString:[news image]];

    // memory management
    [imageFilePath          release];
    [image                  release];
    [cbColor2               release];
    [highlightedBackground  release];

    // delete the local autorelease pool then dealloc autoreleased objects used
    // by the method
    [pool release];
  }

  // memory management
  [reuseIdentifier release];

  return self;
}

- (void) dealloc {
  [super dealloc];

  [_imageConnection cancel];

  [_imageConnection release];
  [_imageData       release];
  [_foo             release];
}

- (void) loadImageFromString:(NSString *) string {
  NSURL *imageURL = [[NSURL alloc] initWithString:string];
  NSURLRequest *imageRequest = [[NSURLRequest alloc] initWithURL:imageURL];

  _imageConnection = [[NSURLConnection alloc] initWithRequest:imageRequest
                                                     delegate:self];

  [_imageConnection start];

  // memory management
  [imageURL     release];
  [imageRequest release];
}

//==============================================================================
// NSURLConnectionDelegate
//==============================================================================

- (void) connection:(NSURLConnection *) connection
     didReceiveData:(NSData *) data {
  //update the temporary datas
  if(!_imageData) {
    _imageData = [[NSMutableData alloc] init];

    [_imageData setData:data];
  } else {
    [_imageData appendData:data];
  }
}

- (void) connection:(NSURLConnection *) connection
   didFailWithError:(NSError *) error {
  // memory management
  [_imageData release];
  _imageData = nil;
}

- (void) connectionDidFinishLoading:(NSURLConnection *) connection {
  if(_imageData) {
    UIImage *image = [[UIImage alloc] initWithData:_imageData];

    [[self imageView] setImage:image];

    // memory management
    [image release];
  }

  // memory management
  [_imageData release];
  _imageData = nil;
}

@end

